
package com.skytech.chatim.sky.task;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.TreeMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.skytech.chatim.sky.util.SystemException;

public class JsonUtil {

    private static final String ENCODING = "utf-8";

	public static HashMap getMapFromJSONObject(JSONObject jsonObject) {
        HashMap<String, String> data = new HashMap<String, String>();
        try {
            Iterator it = jsonObject.keys();
            while (it.hasNext()) {
                String key = it.next().toString();
                String value = jsonObject.getString(key);
                data.put(key, value);
            }
        } catch (JSONException e) {
            throw new SystemException(e);
        }
        return data;
    }

    public static boolean isJsonObject(String body) {
        if (body.startsWith("{")) {
            return true;
        } else {
            return false;
        }
    }

    public static JSONArray getJSONArrayByString(String body) {
        try {
            JSONArray jsonArray = new JSONArray(body);
            return jsonArray;
        } catch (JSONException e) {
            e.printStackTrace();
            throw new SystemException(e);
        }
    }




    public static JSONObject getJSONObjectByString(String body) {
        try {
            JSONObject jsonObject = new JSONObject(body);
            return jsonObject;
        } catch (JSONException e) {
            e.printStackTrace();
            throw new SystemException(e);
        }

    }

    public static ArrayList getArrayListFromJsonString(String body) {
        try {
            ArrayList arrayList = new ArrayList();
            JSONObject jsonObject = new JSONObject(body);
            jsonObject = jsonObject.getJSONObject("list");
            Iterator it = jsonObject.keys();
            while (it.hasNext()) {
                String key = it.next().toString();
                JSONObject subJSONObject = jsonObject.getJSONObject(key);
                HashMap map = JsonUtil.getMapFromJSONObject(subJSONObject);
                arrayList.add(map);
            }
            return arrayList;
        } catch (JSONException e) {
            e.printStackTrace();
            throw new SystemException(e);
        }
    }

    public static HashMap getMapFromJSONObjectString(String jsonString) {
        try {
            JSONObject jsonObject = new JSONObject(jsonString);
            return JsonUtil.getMapFromJSONObject(jsonObject);
        } catch (JSONException e) {
            e.printStackTrace();
            throw new SystemException(e);
        }
    }

    public static String getJsonStringFromMap(HashMap map) {
        JSONObject jsonObject = new JSONObject(map);
        String string = jsonObject.toString();
        return string;
    }


    
    public static ArrayList getSortArrayListFromJsonObject(JSONObject result) {
        try {
            ArrayList arrayList = new ArrayList();
            JSONObject jsonObject;
            try {
                jsonObject = result.getJSONObject("list");
            } catch (Exception e) {
                return arrayList;
            }

            Iterator it = jsonObject.keys();
            TreeMap treeMap = new TreeMap();
            while (it.hasNext()) {
                String key = it.next().toString();
                Integer integerKey = Integer.parseInt(key);
                treeMap.put(integerKey, jsonObject.getJSONObject(key));
            }
            it = treeMap.keySet().iterator();
            while (it.hasNext()) {
                String key = it.next().toString();
                JSONObject subJSONObject = jsonObject.getJSONObject(key);
                HashMap map = JsonUtil.getMapFromJSONObject(subJSONObject);
                arrayList.add(map);
            }
            Collections.reverse(arrayList);
            return arrayList;
        } catch (JSONException e) {
            e.printStackTrace();
            throw new SystemException(e);
        }
    }
    
    public static ArrayList getArrayListFromJsonArray  (JSONObject result) {
        try {
            ArrayList arrayList = new ArrayList();
            JSONArray jsonArray;
            try {
                jsonArray = result.getJSONArray("list");
            } catch (Exception e) {
                return arrayList;
            }

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject subJSONObject  =(JSONObject)jsonArray.get(i);
                HashMap map = JsonUtil.getMapFromJSONObject(subJSONObject);
                arrayList.add(map);
            }
            return arrayList;
        } catch (JSONException e) {
            e.printStackTrace();
            throw new SystemException(e);
        }
    }

    public static ArrayList getArrayListFromJsonObject(JSONObject result) {
        try {
            ArrayList arrayList = new ArrayList();
            JSONObject jsonObject;
            try {
                jsonObject = result.getJSONObject("list");
            } catch (Exception e) {
                return arrayList;
            }
            Iterator it = jsonObject.keys();
            while (it.hasNext()) {
                String key = it.next().toString();
                JSONObject subJSONObject = jsonObject.getJSONObject(key);
                HashMap map = JsonUtil.getMapFromJSONObject(subJSONObject);
                arrayList.add(map);
            }
            return arrayList;
        } catch (JSONException e) {
            e.printStackTrace();
            throw new SystemException(e);
        }
    }



    public static String[] getStringArray(JSONArray jsonArray) {
        if (jsonArray == null){
            return new String[0] ;
        }
        String[] returnStrnig = new String[jsonArray.length()];
        try {
            for (int i = 0; i < jsonArray.length(); i++) {
                returnStrnig[i] = jsonArray.getString(i);
            }
        } catch (JSONException e) {
            throw new SystemException(e);
        }
        return returnStrnig;
    }

    public static String[] getLoginData(AppServerResponse response) {
        // TODO Auto-generated method stub
        return null;
    }



}
